package com.example.recordingpen_datacollection.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.recordingpen_datacollection.Utils.String.StringUtils;
import com.example.recordingpen_datacollection.entity.Businessdata;
import com.example.recordingpen_datacollection.entity.DTO.BusinessdataDto;
import com.example.recordingpen_datacollection.entity.Vo.BusinesMaxTimeVo;
import com.example.recordingpen_datacollection.entity.Vo.BusinesVo;
import com.example.recordingpen_datacollection.redisService.RedisService;
import com.example.recordingpen_datacollection.result.RLIST;
import com.example.recordingpen_datacollection.service.BusinessdataService;
import com.example.recordingpen_datacollection.service.UserLoginService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xg
 * @since 2023-11-29
 */
@RestController
@RequestMapping("/businessdata")
public class BusinessdataController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private BusinessdataService businessdataService;
    @Autowired
    private UserLoginService userLoginService;

    @ApiOperation(value = "查询用户用量，时间")
    @GetMapping("/getBusines")
    public RLIST getBusines(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer value = (Integer) redisService.getValue(token);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("login_id",value);
        Businessdata one = businessdataService.getOne(wrapper);
        BusinessdataDto businessdataDto = new BusinessdataDto();
        businessdataDto.setId(one.getId());
        businessdataDto.setLoginId(one.getLoginId());
        businessdataDto.setChargeType(one.getChargeType());
        businessdataDto.setDosage(one.getDosage());
        businessdataDto.setUaddtime(one.getUaddtime());
        businessdataDto.setAltertime(one.getAltertime());
        businessdataDto.setStartTime(one.getStartTime());
        businessdataDto.setEndTime(one.getEndTime());

        return RLIST.ok().data("data",businessdataDto);
    }

    @ApiOperation(value = "修改设备时间")
    @PostMapping("/setBusinesTime")
    public RLIST setBusinesMaxTime(@RequestBody BusinesMaxTimeVo businesMaxTimeVo, HttpServletRequest request){
        if(StringUtils.isEmpty(businesMaxTimeVo.getTime())){
            return RLIST.error().message("时间不能为空");
        }
        if(businesMaxTimeVo.getDevId() == null || businesMaxTimeVo.getDevId()<=0){
            return RLIST.error().message("id异常");
        }
        String token = request.getHeader("token");
        if( !(token.length() == 51 || token.length() == 56) ){
            return RLIST.error().message("无权操作");
        }
        Integer value =null;
        if(token.length() == 51){
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("uid",value);
            wrapper.eq("devid",businesMaxTimeVo.getDevId());
            int count = userLoginService.count(wrapper);
            if(count<=0){
                return RLIST.error().message("账号不存在");
            }
            value = (Integer) redisService.getValue(token);
        }
        boolean flat = businessdataService.alterBusinesMaxTime(value,businesMaxTimeVo);
        if(flat){
            return RLIST.ok();
        } else {
            return RLIST.error();
        }

    }

}

